package com.nghiatran.democonnection;


import com.nghiatran.democonnection.GetDataService.GetDataHttp;
import com.nghiatran.networks.HttpHelper;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
 * @author TRAN NGHIA 
 * @Email  tranducnghiabkhn@gmail.com
 */

public class DemoConnection extends Activity {
    TextView txt;
    Button btn;
    public String kq;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_demo_connection);
		txt = (TextView) findViewById(R.id.txt_showhang);
		btn = (Button) findViewById(R.id.btn_Submit);
		
		
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			    new GetDataHttp().execute();
			    txt.setText(kq);
			    
 	
			}
		});
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.demo_connection, menu);
		return true;
	}

	public class GetDataHttp extends AsyncTask<Void, String, Void> {
		private int count = 0;
		private Handler handler;
		
		@Override
		protected void onPreExecute() {
			handler = new Handler();
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {
			//Load Data from here
	      		
			try {
					
				kq = HttpHelper.getData("http://onlineworks.vn/demolumi");
				Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

				Log.i(GetDataHttp.class.getName(), "get data"+ kq);
	      
			
			return null;
		}

		@Override
		protected void onProgressUpdate(String... values) {
			String ketqua = values[0];
			Intent intent = new Intent("com.nghiatran.DemoConnections");
			Bundle extras = new Bundle();
			extras.putString("ketqua", ketqua);
			intent.putExtras(extras);
			sendBroadcast(intent);
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(Void result) {
			Log.e(GetDataHttp.class.getName(), "finished download");
			super.onPostExecute(result);
		}

	}

}
